<template>
	<view style="height: 100vh; background-color: #FFFFFF;">
		<u-navbar title-color="#FFFFFF" height="48" back-icon-color="#FFFFFF" :border-bottom="false" :is-fixed="true" :title="'巡检数据'"></u-navbar>
		<u-form ref="inspectionForm" :model="inspectionForm" label-width="100px" class="wrap">
			<u-form-item v-if="equipmentId=='null'" label="企业名称" prop="enterpriseName" >
				<u-input :border="border" type="select" :select-open="enterpriseShow" v-model="inspectionForm.enterpriseName" placeholder="请选择企业" @click="enterpriseShow=true"></u-input>
			</u-form-item>
			<u-form-item v-if="equipmentId=='null'" label="设备名称" prop="equipmentName" >
				<u-input :border="border" type="select" :select-open="equipmentShow" v-model="inspectionForm.equipmentName" placeholder="请选择设备" @click="equipmentShow=true"></u-input>
			</u-form-item>
			<u-form-item v-if="equipmentId!='null'" label="设备名称" >
				<u-input disabled v-model="equipmentInfo.name"></u-input>
			</u-form-item>
			<u-form-item label="设备状态"  prop="equipmentStatusName" >
				<u-input :border="border" type="select" :select-open="statusShow" v-model="inspectionForm.equipmentStatusName" placeholder="请选择状态" @click="statusShow=true"></u-input>
			</u-form-item>
			<u-form-item  label="处置意见" prop="disposalOpinions" >
				<u-input :border="border" v-model="inspectionForm.disposalOpinions" placeholder="请输入维护意见"></u-input>
			</u-form-item>
			<u-form-item label-position="left" label="巡检描述" prop="inspectionRemark" >
				<u-input type="textarea" :border="border" placeholder="请填写巡检描述" v-model="inspectionForm.inspectionRemark" />
			</u-form-item>
			<u-button @click="submit" type="primary">提交</u-button>
		</u-form>
		
		<!--选择器内容-->
		<u-select mode="single-column" :list="enterpriseList" value-name="ucId" label-name="name"  v-model="enterpriseShow" @confirm="enterpriseConfirm"></u-select>
		
		<u-select mode="single-column" :list="equipmentList" value-name="ucId" label-name="name"  v-model="equipmentShow" @confirm="equipmentConfirm"></u-select>
		
		<u-select mode="single-column" :list="statusList"  v-model="statusShow" @confirm="statusConfirm"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				equipmentId:"",
				equipmentInfo:{},
				inspectionForm:{},
				border:true,
				enterpriseShow:false,
				enterpriseList:[],
				equipmentShow:false,
				equipmentList:[],
				statusShow:false,
				statusList:[
					{
						label:"正常",
						value:0
					},
					{
						label:"损坏",
						value:1
					},
					{
						label:"停机",
						value:2
					}
				],
				rules:{
					enterpriseName: [
						// 对name字段进行必填验证
						{
							required: true,
							message: '请选择企业',
							trigger: ['change','blur']
						},
					],
					equipmentName: [
						// 对name字段进行必填验证
						{
							required: true,
							message: '请选择设备',
							trigger: ['change','blur']
						},
					],
					equipmentStatusName: [
						// 对name字段进行必填验证
						{
							required: true,
							message: '请选择设备状态',
							trigger: ['change','blur']
						},
					],
					inspectionRemark:[
						{
							required: true,
							message: '请输入巡检描述',
							trigger: ['change','blur']
						},
					]
				}
				
			}
		},
		onReady() {
			this.$refs.inspectionForm.setRules(this.rules);
		},
		onLoad(e) {
			console.log(e)
			this.equipmentId = e.ucId
		},
		onShow() {
			this.initViews()
		},
		methods: {
			initViews(){
				if(this.equipmentId!="null"){
					this.getEquipmentInfo()
					
				}else{
					this.getEnterpriseList()
					this.getEquipmentList()
				}
			},
			getEquipmentInfo(){
				this.utils.request("/api/app/equipment/getInEquipmentVo", {deleted:false,ucId: this.equipmentId}, "post", "app", true).then(res => {
					this.equipmentInfo = res.data
					this.inspectionForm.equipmentId = this.equipmentInfo.ucId
					this.inspectionForm.enterpriseId = this.equipmentInfo.enterpriseId
				}).catch(error => {
					console.log(error)
				})
			},
			// 维保企业id
			getEnterpriseList() {
				this.utils.request("/api/app/enterpriseDominant/getInEnterpriseDominantList", {deleted:false}, "post", "app", true).then(res => {
					this.enterpriseList = res.data
				}).catch(error => {
					console.log(error)
				})
			},
			// 设备id
			getEquipmentList(val) {
				this.utils.request("/api/app/equipment/getEquipmentList", {enterpriseId:val,}, "post", "app", true).then(res => {
					this.equipmentList=res.data
				}).catch(error => {
					console.log(error)
				})
			},
			enterpriseConfirm(e) {
				e.map((val, index) => {
					this.inspectionForm.enterpriseName = val.label
					this.inspectionForm.enterpriseId = val.value
					this.getEquipmentList(val.value)
				})
			},
			equipmentConfirm(e){
				e.map((val, index) => {
					this.inspectionForm.equipmentName = val.label
					this.inspectionForm.equipmentId = val.value
				})
			},
			statusConfirm(e){
				e.map((val, index) => {
					this.inspectionForm.equipmentStatusName = val.label
					this.inspectionForm.equipmentStatus = val.value
				})
			},
			submit(){
				this.$refs.inspectionForm.validate(valid => {
					if (valid) {
						this.utils.request("/api/app/Inspection/addInEquipmentInspection", this.inspectionForm, "post", "app", true).then(res => {
							console.log(`返回结果：`, res)
							uni.navigateBack({
								delta:1
							})
						}).catch(error => {
							console.log(error)
						})
					} else {
						console.log('验证失败');
					}
				});
			}
		}
	}
</script>

<style>

</style>
